#!/bin/bash
set -e

if (( ${#} < 2 )); then
  echo
  echo "$0 <net> <vm_name>"
  echo
  exit 1
fi

cloonix_name=$1
vm_name=$2

if [ -z $CLOONIX_CONFIG ]; then
  CLOONIX_CONFIG=/usr/local/bin/cloonix/cloonix_config
fi

if [ ! -f $CLOONIX_CONFIG ]; then
  echo "ERROR: $CLOONIX_CONFIG not found"
  exit 1
fi

LIST=$(cat $CLOONIX_CONFIG |grep CLOONIX_NET: | awk '{print $2}')

if (( ${#} < 1 )); then
  echo
  echo "$0 <net>"
  echo
  echo net:
  echo $LIST
  echo
  exit 1
fi
cloonix_name=$1
found=no
for i in $LIST ; do
  if [ "$cloonix_name" == "$i" ]; then
    found=yes
  fi
done

if [ "$found" == "no" ]; then
  echo "ERROR: $cloonix_name not found in $CLOONIX_CONFIG"
  echo $LIST
  exit 1
fi


DIST_TREE=$(cloonix_cli $cloonix_name dcf |grep  CLOONIX_TREE | awk -F = '{print $2}')
DIST_WORK=$(cloonix_cli $cloonix_name dcf |grep  CLOONIX_WORK | awk -F = '{print $2}')

echo
echo "  Use Ctrl-\ to quit"
echo "  ============================="
echo
sleep 1

cloonix_xwy $cloonix_name -cmd ${DIST_TREE}/server/dtach/dtach -a ${DIST_WORK}/dtach/$vm_name

